package com.lqs.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lqs.blog.mapper.ArticleTagMapper;
import com.lqs.blog.mapper.TagMapper;
import com.lqs.blog.pojo.Article;
import com.lqs.blog.pojo.ArticleTag;
import com.lqs.blog.pojo.Tag;
import com.lqs.blog.service.TagService;
import com.lqs.blog.vo.Result;
import com.lqs.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Override
    public List<TagVo> getTagsByArticleId(Long articleId) {
        ArrayList<TagVo> tags = new ArrayList<>();
        //mybatisPlus无法进行多表查询
        List<Tag> tagList = tagMapper.getTagsByArticleId(articleId);

        for (Tag tag : tagList) {
            tags.add(copy(tag));
        }
        return tags;
    }

    /**
     * 得到前limit个所有tagId
     *
     * @param limit
     * @return
     */
    @Override
    public List<Long> getTagIds(int limit) {
        return tagMapper.getTagIds(limit);
    }

    private TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag, tagVo);
        tagVo.setId(tag.getId().toString());
        return tagVo;
    }

    /**
     * 通过ids查询标签
     *
     * @param tagIds
     * @return
     */
    @Override
    public List<Tag> getAllTags(List<Long> tagIds) {
        return tagMapper.getAllTags(tagIds);
    }

    @Override
    public Result hots(int limit) {
        /**
         * 1.标签所拥有的文章数量最多 最热标签
         * 2.查询根据tag_id分组 计数 排序 取前limit个
         */
        List<Long> tagIds = tagMapper.getHotTagIds(limit);
        //需求的时tag_id 和tag_name
        //ids不能为空需要判断
        //select * from ms_tag where id in (1,2,3)
        if (CollectionUtils.isEmpty(tagIds)) {
            //如果为空则返回空的对象集合
            return Result.success(Collections.emptyList());
        }
        List<Tag> tags = tagMapper.getTagsByIds(tagIds);
        return Result.success(tags);
    }

    @Override
    public Result getTags() {
        List<Tag> tagList = tagMapper.selectList(new LambdaQueryWrapper<>());
        List<TagVo> tagVos = copyList(tagList);
        return Result.success(tagVos);
    }

    private List<TagVo> copyList(List<Tag> tagList) {
        List<TagVo> tagVos = new ArrayList<>();
        for (Tag tag : tagList) {
            tagVos.add(copy(tag));
        }
        return tagVos;
    }

    @Override
    public Result getTagDetail() {
        List<Tag> tagList = tagMapper.selectList(new LambdaQueryWrapper<>());
        List<TagVo> tagVos = copyList(tagList);
        return Result.success(tagVos);
    }

    @Override
    public Result getTagById(Long id) {
        Tag tag = tagMapper.selectById(id);

        return Result.success(copy(tag));
    }

    @Override
    public List<ArticleTag> getTagsByTagId(Long tagId) {
        LambdaUpdateWrapper<ArticleTag> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.eq(ArticleTag::getTagId, tagId);
        return articleTagMapper.selectList(lambdaUpdateWrapper);
    }
}
